library(tidyverse)
## ── Attaching packages ────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(rvest)
## Loading required package: xml2
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:purrr':
## 
##     pluck
## The following object is masked from 'package:readr':
## 
##     guess_encoding
library(readr)
library(viridis)
## Loading required package: viridisLite
library(leaflet)
library(patchwork)
library(RColorBrewer)
library(corrplot)
## corrplot 0.84 loaded
knitr::opts_chunk$set(
    echo = TRUE,
    warning = FALSE,
    fig.width = 8, 
  fig.height = 6,
  out.width = "90%"
)
options(
  ggplot2.continuous.colour = "viridis",
  ggplot2.continuous.fill = "viridis"
)
scale_colour_discrete = scale_colour_viridis_d
scale_fill_discrete = scale_fill_viridis_d
theme_set(theme_minimal() + theme(legend.position = "bottom"))
data_2018 = 
  read_csv("./data/2018data.csv") %>% 
  janitor::clean_names() 
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   TIME = col_time(format = ""),
##   `ZIP CODE` = col_double(),
##   LATITUDE = col_double(),
##   LONGITUDE = col_double(),
##   `NUMBER OF PERSONS INJURED` = col_double(),
##   `NUMBER OF PERSONS KILLED` = col_double(),
##   `NUMBER OF PEDESTRIANS INJURED` = col_double(),
##   `NUMBER OF PEDESTRIANS KILLED` = col_double(),
##   `NUMBER OF CYCLIST INJURED` = col_double(),
##   `NUMBER OF CYCLIST KILLED` = col_double(),
##   `NUMBER OF MOTORIST INJURED` = col_double(),
##   `NUMBER OF MOTORIST KILLED` = col_double(),
##   COLLISION_ID = col_double()
## )
## See spec(...) for full column specifications.
newnames = colnames(data_2018) %>% 
  str_replace("number_of_","") 
names(data_2018) = newnames

tidy_data = 
data_2018 %>% 
  mutate(
    date_complete = date 
  ) %>% 
  separate(date, into = c("month", "day", "year"), sep = "/") %>% 
  separate(time, into = c("hour", "minute"), sep = ":") %>%
  select(-zip_code, -location, -on_street_name, -cross_street_name, -off_street_name,-collision_id,-year) %>% 
  rename("vehicle_type" = "vehicle_type_code_1") %>% 
  mutate( day = as.numeric(day),
          month = as.numeric(month),
          hour = as.numeric(hour),
          minute = as.numeric(minute),
          latitude = replace_na(latitude,0),
          vehicle_type = str_to_lower(vehicle_type)
  ) %>%
  filter( latitude != 0)

Vehicle type

vehicle_type_data = 
  tidy_data %>% 
  mutate(
    vehicle_type = replace(vehicle_type,str_detect(vehicle_type,"truck"),"truck"),
    vehicle_type = replace(vehicle_type,str_detect(vehicle_type,"sport utility"),"sport utility vehicle")
    ) %>% 
  filter( vehicle_type %in% c("taxi","passenger vehicle","truck","sport utility vehicle")) %>% 
  group_by(vehicle_type,hour) %>% 
  summarize(
    n = n()
  )

vehicle_type_data %>% 
  plot_ly(
    x = ~hour, y = ~n, color = ~vehicle_type, type = "scatter", mode = "line") %>% 
  layout(
    title = "Collisions of Day for Different Vehicles",
    xaxis = list(title = "Hour of Day"),
    yaxis = list(title = "Collisions")
    )

Top 8 Collision Reasons

reason_data = 
  tidy_data %>%
  group_by(contributing_factor_vehicle_1) %>%
  summarize(n = n()) %>% 
  arrange(desc(n)) %>% 
  head(10)

reason_data %>% 
  plot_ly(x = ~reorder(contributing_factor_vehicle_1,desc(n)), y = ~n, color = ~contributing_factor_vehicle_1 ,type = "bar") %>% 
  layout(
    title = "The Number of Items Ordered in Each Aisle",
    xaxis = list(title = "Different Reasons"),
    yaxis = list(title = "Count")
    )

Mapping

data_2018 = tidy_data

data_2018 = rename(data_2018, long = latitude, lat = longitude)

pal <- colorNumeric(
  palette = "viridis",
  domain = data_2018$persons_injured)

data_2018 %>% 
  filter(!(lat < "-70" | lat >= "-75")) %>% 
  filter(persons_injured > 2) %>% 
    mutate(
    label = str_c("<b>vehicle type: ", vehicle_type, "</b><br>Month: ", month , sep = "") ) %>% 
  sample_n(2000) %>% 
  leaflet() %>% 
  addTiles() %>%
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addLegend("bottomright", pal = pal, values = ~persons_injured,
    title = "Persons Injured",
    opacity = 1
  ) %>% 
  addCircleMarkers(
    ~lat, ~long,
    color = ~pal(persons_injured),
    radius = 0.5,
    popup = ~ label) 
data_2018 %>% 
  group_by(borough) %>% 
  summarise(n())
## # A tibble: 6 x 2
##   borough       `n()`
##   <chr>         <int>
## 1 BRONX         22121
## 2 BROOKLYN      46314
## 3 MANHATTAN     29728
## 4 QUEENS        40400
## 5 STATEN ISLAND  5988
## 6 <NA>          71583
data_2018_seperate = tidy_data 

data_kill_injured = data_2018_seperate %>% 
  select(month, persons_injured,persons_killed, pedestrians_injured, pedestrians_killed, cyclist_injured, cyclist_killed, motorist_injured, motorist_killed)

data_kill_injured$injured  = apply(data_kill_injured[,c(2,4,6,8)],1,sum,na.rm=T)
data_kill_injured$killed = apply(data_kill_injured[,c(3,5,7,9)],1,sum,na.rm=T)

data_kill_injured = 
  data_kill_injured %>% 
  group_by(month) %>% 
  summarise(
    sum_injured = sum(injured),
    sum_killed = sum(killed)
  ) %>%
  ungroup() 
  
data_kill_injured = data_kill_injured %>% 
  pivot_longer(
    sum_injured:sum_killed,
    names_to = "type",
    values_to = "number"
  ) 
plot_kill_injured = data_kill_injured %>% 
  ggplot(aes(x = month, y = number, color = type))+
           geom_point()+
           geom_line()+
  scale_x_continuous(breaks=seq(1, 12, 1),
                     labels = c("Jan", "Feb", "Mar", "Apr", "May",
                                "Jun","Jul", "Aug", "Sep", "Oct",
                                "Nov", "Dec"))+
  scale_y_continuous(breaks = seq(0,11000,1000))+
  labs(
   title = "Trend of People being Injured or Killed through the Year")+
  theme(axis.title = element_text(size=14,face="bold"),
        plot.title = element_text(hjust = 0.5,color ="Blue"))


plot_kill_injured = ggplotly(plot_kill_injured)
plot_kill_injured
data_kill_injured_day = data_2018_seperate %>% 
  select(date_complete, persons_injured,persons_killed, pedestrians_injured, pedestrians_killed, cyclist_injured, cyclist_killed, motorist_injured, motorist_killed)

data_kill_injured_day$injured  = apply(data_kill_injured_day[,c(2,4,6,8)],1,sum,na.rm=T)
data_kill_injured_day$killed = apply(data_kill_injured_day[,c(3,5,7,9)],1,sum,na.rm=T)

data_kill_injured_day = 
  data_kill_injured_day%>% 
  group_by(date_complete) %>% 
  summarise(
    sum_injured = sum(injured),
    sum_killed = sum(killed)
  ) %>%
  ungroup() 
  
data_kill_injured_day = data_kill_injured_day %>% 
  pivot_longer(
    sum_injured:sum_killed,
    names_to = "type",
    values_to = "number"
  ) %>% 
  mutate(
    day = rep(1:365, each = 2),
    month = rep(1:12,c(62,56,62,60,62,60,62,62,60,62,60,62))
  )

plot_kill_injured_day = data_kill_injured_day %>% 
  ggplot(aes(x = date_complete, y = number, group = type, color = type))+
  geom_line()+
  theme(axis.text.x = element_blank(),
        axis.title = element_text(size=14,face="bold"),
        plot.title = element_text(hjust = 0.5,color ="Blue"))+
  labs(
   title = "Trend of People being Injured or Killed through the Day",
   x = "Day of the Year")
  
plot_kill_injured_day

plot_kill_injured_day = ggplotly(plot_kill_injured_day)
plot_kill_injured_day

proportion of accident and injured people by borough, by hour

boro_plot = tidy_data %>% 
  drop_na(borough, persons_injured, persons_killed) %>%
  group_by(borough) %>%
  summarize(accident = n(),
            injured = sum(persons_injured)) %>%
  mutate(prop_accident = round(accident/sum(accident),4),
         prop_injured = round(injured/sum(injured),4)) %>%
  mutate(text_label1 = str_c("accident: ", accident), 
         text_label2 = str_c("injured: ", injured)) %>% 
  plot_ly(x = ~borough, y = ~prop_accident, type = 'bar', name = 'accident', text = ~text_label1, alpha = 0.8) %>%
  add_trace(x = ~borough, y = ~prop_injured, type = 'bar', name = 'injury', text = ~text_label2, alpha = 0.8) %>%
  layout(title="proportion of accident and injured people by borough", xaxis = list(title = 'borough'), barmode = 'group', legend = list(orientation = "h", xanchor = "center", x = 0.5, y = -0.2)) 
boro_plot

Number of accident and injured people in Brooklyn is greatest comparing with other boroughs. Queen is second. Staten Island have fewest accident and injured people. Manhattan and Bronx is in the middle.

hour_table = tidy_data %>% 
  drop_na(hour, persons_injured, persons_killed) %>%
  group_by(hour) %>%
  summarize(accident = n(),
            injured = sum(persons_injured)) 
hour_plot = hour_table %>%
  mutate(prop_accident = round(accident/sum(accident),4),
         prop_injured = round(injured/sum(injured),4)) %>%
  mutate(text_label1 = str_c("accident: ", accident), 
         text_label2 = str_c("injured: ", injured)) %>% 
  plot_ly(x = ~hour, y = ~prop_accident, type = 'scatter', mode = 'lines', name = 'accident', text = ~text_label1, alpha = 0.8) %>%
  add_trace(x = ~hour, y = ~prop_injured, type = 'scatter', mode = 'lines', name = 'injury', text = ~text_label2, alpha = 0.8) %>%
  layout(title="proportion of accident and injured people by hour", xaxis = list(autotick = FALSE, ticks = "outside", tick0 = 0, dtick = 1, title = 'Hour'), yaxis = list(autotick = FALSE, ticks = "outside", tick0 = 0, dtick = 0.01, title = 'proportion'), legend = list(orientation = "h", xanchor = "center", x = 0.5, y = -0.2)) 

hour_plot %>%
  layout(shapes = list(type = "rect", fillcolor = "pink", line = list(color = "pink"), opacity = 0.3, x0 = 8, x1 = 19, xref = "x", y0 = 0, y1 = 0.08, yref = "y"))

Frequency of accident and injured people in the period of 8 am-19 pm is higher than other period of time. Started from 3 am, number of accident and injured people increase and reach a small peak at 8am. At 5pm, it reaches a big peak. then it started to decrease.

Holiday Analysis

data_2018 = tidy_data

holiday_data = 
  data_2018 %>%
  select(date_complete, everything()) %>%
  mutate(holiday = "No Holiday") %>% 
  select(date_complete, holiday, everything()) %>% 
  mutate(holiday = replace(holiday, date_complete == "01/01/2018", "New Year's Day"), 
         holiday = replace(holiday, date_complete == "01/15/2018", "Martin Luther King, Jr. Day"),
         holiday = replace(holiday, date_complete == "01/13/2018"|date_complete =="01/14/2018", "Martin Luther King Day Weekend (2 days)"),
         holiday = replace(holiday, date_complete == "02/19/2018", "Presidents Day"),
         holiday = replace(holiday, date_complete == "02/17/2018"|date_complete =="02/18/2018", "Presidents Day Weekend (2 days)"),
         holiday = replace(holiday, date_complete == "05/28/2018", "Memorial Day"),
         holiday = replace(holiday, date_complete == "05/26/2018"|date_complete =="05/27/2018", "Memorial Day Weekend (2 days)"),
         holiday = replace(holiday, date_complete == "07/04/2018", "Independence Day"),
         holiday = replace(holiday, date_complete == "07/07/2018"|date_complete =="07/08/2018", "Independence Day Weekend (2 days)"),
         holiday = replace(holiday, date_complete == "09/03/2018", "Labor Day"),
         holiday = replace(holiday, date_complete == "09/01/2018"|date_complete =="09/02/2018", "Labor Day Weekend (2 days)"),
         holiday = replace(holiday, date_complete == "11/22/2018", "Thanksgiving Day"),
         holiday = replace(holiday, date_complete == "11/23/2018"|date_complete =="11/24/2018"|date_complete =="11/25/2018", "Thanksgiving Weekend (3 days)"),
         holiday = replace(holiday, date_complete == "12/25/2018", "Christmas Day"),
         holiday = replace(holiday, date_complete == "12/26/2018"|date_complete =="12/27/2018", "Christmas Day Weekend (2 days)")
         )
holiday_data =
  holiday_data %>% 
  drop_na(persons_injured)

holiday_data %>% 
  group_by(holiday) %>% 
  summarise(total_injured = sum(persons_injured))
## # A tibble: 16 x 2
##    holiday                                 total_injured
##    <chr>                                           <dbl>
##  1 Christmas Day                                      93
##  2 Christmas Day Weekend (2 days)                    286
##  3 Independence Day                                  130
##  4 Independence Day Weekend (2 days)                 330
##  5 Labor Day                                         216
##  6 Labor Day Weekend (2 days)                        333
##  7 Martin Luther King Day Weekend (2 days)           279
##  8 Martin Luther King, Jr. Day                       151
##  9 Memorial Day                                      156
## 10 Memorial Day Weekend (2 days)                     357
## 11 New Year's Day                                    122
## 12 No Holiday                                      54633
## 13 Presidents Day                                    103
## 14 Presidents Day Weekend (2 days)                   276
## 15 Thanksgiving Day                                  109
## 16 Thanksgiving Weekend (3 days)                     382
bronx = 
  holiday_data %>% 
  filter(borough == "BRONX") %>% 
  group_by(holiday) %>% 
  summarise(bronx_total_injured = sum(persons_injured))
brooklyn = 
  holiday_data %>% 
  filter(borough == "BROOKLYN") %>% 
  group_by(holiday) %>% 
  summarise(brooklyn_total_injured = sum(persons_injured)) %>% 
  select(brooklyn_total_injured)
manhattan = 
  holiday_data %>% 
  filter(borough == "MANHATTAN") %>% 
  group_by(holiday) %>% 
  summarise(manhattan_total_injured = sum(persons_injured))%>% 
  select(manhattan_total_injured)
queens = 
  holiday_data %>% 
  filter(borough == "QUEENS") %>% 
  group_by(holiday) %>% 
  summarise(queens_total_injured = sum(persons_injured))%>% 
  select(queens_total_injured)
staten_island = 
  holiday_data %>% 
  filter(borough == "STATEN ISLAND") %>% 
  group_by(holiday) %>% 
  summarise(staten_island_total_injured = sum(persons_injured))%>% 
  select(staten_island_total_injured)

holiday_borough = 
  cbind(bronx, brooklyn, manhattan, queens, staten_island) %>% 
  mutate(total = bronx_total_injured+brooklyn_total_injured+manhattan_total_injured+    queens_total_injured+staten_island_total_injured) %>% 
  filter(holiday!="No Holiday")

knitr::kable(holiday_borough)
holiday bronx_total_injured brooklyn_total_injured manhattan_total_injured queens_total_injured staten_island_total_injured total
Christmas Day 10 25 5 24 6 70
Christmas Day Weekend (2 days) 34 99 23 41 3 200
Independence Day 18 32 3 18 1 72
Independence Day Weekend (2 days) 26 60 38 54 6 184
Labor Day 3 60 13 32 3 111
Labor Day Weekend (2 days) 31 89 10 62 7 199
Martin Luther King Day Weekend (2 days) 23 58 40 53 7 181
Martin Luther King, Jr. Day 27 42 14 19 4 106
Memorial Day 16 38 12 24 3 93
Memorial Day Weekend (2 days) 36 70 27 67 7 207
New Year’s Day 10 34 9 14 1 68
Presidents Day 5 32 8 32 1 78
Presidents Day Weekend (2 days) 30 42 28 51 4 155
Thanksgiving Day 13 17 5 17 0 52
Thanksgiving Weekend (3 days) 42 74 39 93 4 252
holiday_borough_day = 
  holiday_borough %>% 
  filter(holiday =="Christmas Day"|holiday =="Independence Day"|holiday =="Labor Day"|holiday =="Martin Luther King, Jr. Day"|holiday =="Memorial Day"|holiday =="New Year’s Day"|holiday =="Thanksgiving Day"|holiday =="Presidents Day")

knitr::kable(holiday_borough_day)
holiday bronx_total_injured brooklyn_total_injured manhattan_total_injured queens_total_injured staten_island_total_injured total
Christmas Day 10 25 5 24 6 70
Independence Day 18 32 3 18 1 72
Labor Day 3 60 13 32 3 111
Martin Luther King, Jr. Day 27 42 14 19 4 106
Memorial Day 16 38 12 24 3 93
Presidents Day 5 32 8 32 1 78
Thanksgiving Day 13 17 5 17 0 52
holiday_borough_day = holiday_borough_day[order(holiday_borough_day$total),]

# On the day of the holiday

plot_day =
pivot_longer(
    holiday_borough_day, 
    bronx_total_injured:staten_island_total_injured,
    names_to = "borough", 
    values_to = "person_injured") %>% 
  ggplot(aes(x = reorder(holiday, -total), y = person_injured, fill = borough)) +
  geom_bar(stat="identity") +
  theme(legend.position = "right", axis.text.x = element_text(angle = 45)) +
  labs(
    title = "Persons Injured by Car Crashs on Federal Holidays (2018)",
    x = "Holiday",
    y = "# Persons Injured") 
# scale_fill_discrete(name = "Borough", labels = c("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island")) 

On three day weekends

data_2018 = tidy_data

holiday_data_weekend = 
  data_2018 %>%
  select(date_complete, everything()) %>%
  mutate(holiday = "No Holiday") %>% 
  select(date_complete, holiday, everything()) %>% 
  mutate(holiday = replace(holiday, date_complete == "01/15/2018"|date_complete == "01/13/2018"|date_complete =="01/14/2018", "Martin Luther King Day Weekend (3 days)"),
         holiday = replace(holiday, date_complete == "02/17/2018"|date_complete == "02/19/2018"|date_complete =="02/18/2018", "Presidents Day Weekend (3 days)"),
         holiday = replace(holiday, date_complete == "05/28/2018"|date_complete == "05/26/2018"|date_complete =="05/27/2018", "Memorial Day Weekend (3 days)"),
         holiday = replace(holiday, date_complete == "07/04/2018"|date_complete == "07/07/2018"|date_complete =="07/08/2018", "Independence Day Weekend (3 days)"),
         holiday = replace(holiday, date_complete == "09/03/2018"|date_complete == "09/01/2018"|date_complete =="09/02/2018", "Labor Day Weekend (3 days)"),
         holiday = replace(holiday, date_complete == "11/22/2018"|date_complete == "11/23/2018"|date_complete =="11/24/2018"|date_complete =="11/25/2018", "Thanksgiving Weekend (4 days)"),
         holiday = replace(holiday, date_complete == "12/25/2018"|date_complete == "12/26/2018"|date_complete =="12/27/2018", "Christmas Day Weekend (3 days)")
         )

holiday_data_weekend =
  holiday_data_weekend %>% 
  drop_na(persons_injured)

holiday_data_weekend %>% 
  group_by(holiday) %>% 
  summarise(total_injured = sum(persons_injured))
## # A tibble: 8 x 2
##   holiday                                 total_injured
##   <chr>                                           <dbl>
## 1 Christmas Day Weekend (3 days)                    379
## 2 Independence Day Weekend (3 days)                 460
## 3 Labor Day Weekend (3 days)                        549
## 4 Martin Luther King Day Weekend (3 days)           430
## 5 Memorial Day Weekend (3 days)                     513
## 6 No Holiday                                      54755
## 7 Presidents Day Weekend (3 days)                   379
## 8 Thanksgiving Weekend (4 days)                     491
bronx = 
  holiday_data_weekend %>% 
  filter(borough == "BRONX") %>% 
  group_by(holiday) %>% 
  summarise(bronx_total_injured = sum(persons_injured))
brooklyn = 
  holiday_data_weekend %>% 
  filter(borough == "BROOKLYN") %>% 
  group_by(holiday) %>% 
  summarise(brooklyn_total_injured = sum(persons_injured)) %>% 
  select(brooklyn_total_injured)
manhattan = 
  holiday_data_weekend %>% 
  filter(borough == "MANHATTAN") %>% 
  group_by(holiday) %>% 
  summarise(manhattan_total_injured = sum(persons_injured))%>% 
  select(manhattan_total_injured)
queens = 
  holiday_data_weekend %>% 
  filter(borough == "QUEENS") %>% 
  group_by(holiday) %>% 
  summarise(queens_total_injured = sum(persons_injured))%>% 
  select(queens_total_injured)
staten_island = 
  holiday_data_weekend %>% 
  filter(borough == "STATEN ISLAND") %>% 
  group_by(holiday) %>% 
  summarise(staten_island_total_injured = sum(persons_injured))%>% 
  select(staten_island_total_injured)

holiday_borough_weekend = 
  cbind(bronx, brooklyn, manhattan, queens, staten_island) %>% 
  mutate(total = bronx_total_injured+brooklyn_total_injured+manhattan_total_injured+    queens_total_injured+staten_island_total_injured) %>% 
  filter(holiday!="No Holiday")

knitr::kable(holiday_borough_weekend)
holiday bronx_total_injured brooklyn_total_injured manhattan_total_injured queens_total_injured staten_island_total_injured total
Christmas Day Weekend (3 days) 44 124 28 65 9 270
Independence Day Weekend (3 days) 44 92 41 72 7 256
Labor Day Weekend (3 days) 34 149 23 94 10 310
Martin Luther King Day Weekend (3 days) 50 100 54 72 11 287
Memorial Day Weekend (3 days) 52 108 39 91 10 300
Presidents Day Weekend (3 days) 35 74 36 83 5 233
Thanksgiving Weekend (4 days) 55 91 44 110 4 304
plot_weekend = 
pivot_longer(
    holiday_borough_weekend, 
    bronx_total_injured:staten_island_total_injured,
    names_to = "borough", 
    values_to = "person_injured") %>% 
  ggplot(aes(x = reorder(holiday, -total), y = person_injured, fill = borough)) +
  geom_bar(stat="identity") +
  theme(legend.position = "right", axis.text.x = element_text(angle = 45)) +
  labs(
    title = "Persons Injured by Car Crashs on Federal Holidays Weekends (2018)",
    x = "Holiday Weekends",
    y = "# Persons Injured") +
  scale_fill_discrete(name = "Borough", labels = c("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island"))

plot_day + plot_weekend

Weather

climate_df = 
  read_csv("./large_data/climate.csv") %>% 
  janitor::clean_names() %>% 
  select(date,hourly_dry_bulb_temperature,hourly_precipitation,daily_weather) %>%
  separate(date, into = c("date", "time"), sep = " ") %>% 
  mutate(
    date = as.Date(date)
  )
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   DATE = col_datetime(format = ""),
##   REPORT_TYPE = col_character(),
##   BackupDirection = col_logical(),
##   BackupDistance = col_logical(),
##   BackupDistanceUnit = col_logical(),
##   BackupElements = col_logical(),
##   BackupElevation = col_logical(),
##   BackupElevationUnit = col_logical(),
##   BackupEquipment = col_logical(),
##   BackupLatitude = col_logical(),
##   BackupLongitude = col_logical(),
##   BackupName = col_logical(),
##   DailyPeakWindDirection = col_character(),
##   DailyPrecipitation = col_character(),
##   DailySnowDepth = col_character(),
##   DailySnowfall = col_character(),
##   DailySustainedWindDirection = col_character(),
##   DailyWeather = col_character(),
##   HeavyFog = col_logical(),
##   HourlyPrecipitation = col_character()
##   # ... with 29 more columns
## )
## See spec(...) for full column specifications.
all_day_weather_df =
  climate_df %>% filter(time == "23:59:00") %>% 
  distinct(date,daily_weather) %>% 
  filter(!duplicated(date)) %>% 
  separate(daily_weather, into = c("weathertype", "other"), sep = 2) %>%
  mutate(
    weathertype = recode(weathertype,
                      "SN" = "snow",
                      "HZ" = "haze",
                      "RA" = "rain",
                      "FG" = "fog",
                      "BR" = "mist"
                      ),
    weathertype = replace_na(weathertype,"sunny")) %>% 
  select(-other)

collision_all = 
tidy_data %>% 
  group_by(date_complete) %>% 
  summarise(
    collision_event = n(),
    injured_event = sum(persons_injured != 0, na.rm = TRUE),
    killed_event = sum(persons_killed != 0, na.rm = TRUE)
  )

collision_weather = 
bind_cols(all_day_weather_df,collision_all) %>% 
  select(-date_complete)

Weather type

weather_type_plot =  
collision_weather %>% 
  group_by(weathertype) %>% 
  summarise(
    count = mean(collision_event)
  ) %>% 
  mutate(
    weathertype = fct_reorder(weathertype,count)
  ) %>% 
  ggplot(aes(weathertype, count,fill = weathertype)) + 
  geom_bar(stat = "identity") +
  coord_flip()
  
ggplotly(weather_type_plot)

Daily weather

daily_weather_df = 
climate_df %>% 
  separate(time, into = c("hour", "minute","second"), sep = ":") %>% 
  separate(date, into = c("year","month", "day"), sep = "-") %>% 
  separate(hourly_precipitation, into = c("prep","other"), sep = 4) %>% 
  select(-minute,-second,-year,-daily_weather,-other) %>% 
  mutate(
    month = as.numeric(month),
    day = as.numeric(day),
    hour = as.numeric(hour),
    prep = replace_na(as.numeric(prep),0)
  )

daily_collision_data = 
tidy_data %>% 
  select(month,day,hour,persons_injured,persons_killed) %>% 
  group_by(month,day,hour) %>% 
  summarise(
    eventsnumber = n()
  )

daily_weather_collision = 
  left_join(daily_weather_df,daily_collision_data,
            by = c("month","day","hour")) %>% 
  mutate(
    prep_degree = 
      if_else(prep %in% 0:0.10,"light",
              if_else( prep < 0.3 & prep > 0.11 , "moderate","heavy")),
    prep_degree = fct_relevel(prep_degree, c("heavy","moderate","light"))
  )

daily_weather_plot =
daily_weather_collision %>% 
  ggplot(aes(hour, eventsnumber, color = prep_degree)) +
  geom_point(alpha = 0.5) +
  facet_grid(~ prep_degree) +
  scale_colour_manual(name = "grp",values = brewer.pal(5,"Set2"))

ggplotly(daily_weather_plot)